const { contextBridge, ipcRenderer, shell } = require("electron");

contextBridge.exposeInMainWorld("electron", {
  ipcRenderer: {
    send: (channel, data) => ipcRenderer.send(channel, data),
    on: (channel, func) =>
      ipcRenderer.on(channel, (event, ...args) => func(...args)),
    closeApp: () => ipcRenderer.send("close-app"),
    openExternal: (url) => ipcRenderer.send("open-external", url),
    request: (url, options = {}) => {
      return ipcRenderer.invoke('http-request', url, options);
    }
  },
});

contextBridge.exposeInMainWorld("electronAPI", {
  checkNetworkStatus: () => ipcRenderer.invoke("check-network-status"),
  getClassData: () => ipcRenderer.invoke("get-class-data"),
  saveClassData: (classData) =>
    ipcRenderer.invoke("save-class-data", classData),
  deleteClassData: (classNo) =>
    ipcRenderer.invoke("delete-class-data", classNo),
  saveStudentData: (students) =>
    ipcRenderer.invoke("save-student-data", students),
  getStudentData: () => ipcRenderer.invoke("get-student-data"),
  deleteStudent: (stu_no) => ipcRenderer.invoke("delete-student", stu_no),
  getClassStudentCount: (classNo) =>
    ipcRenderer.invoke("get-student-count", classNo),
  getClassDataByNo: (classNo) =>
    ipcRenderer.invoke("get-class-data-by-no", classNo),
  updateStuData: (stuData) => ipcRenderer.invoke("update-stu-data", stuData),
  getStuDataByClassNo: (classNo) =>
    ipcRenderer.invoke("get-stus-data-by-class-no", classNo),
  addPoints: (data) => ipcRenderer.invoke("add-points", data),
  addAttempts: (data) => ipcRenderer.invoke("add-attempts", data),
  deleteStuData: (data) => ipcRenderer.invoke("delete-student", data),
  openSettings: () => ipcRenderer.send('open-settings-window'),
  openSettingsAndShowLogin: () => ipcRenderer.send('open-settings-and-show-login'), 
  onShowLoginModal: (callback) => ipcRenderer.on('show-login-modal', callback), 
  onWindowMaximized: (callback) => ipcRenderer.on('window-maximized', callback),
  onWindowUnmaximized: (callback) => ipcRenderer.on('window-unmaximized', callback),
  toggleFullscreen: () => ipcRenderer.send('toggle-fullscreen'),
  closeWindow: () => ipcRenderer.send('close-window'),
  hideWindow: () => ipcRenderer.send('hide-window'),
  minimizeWindow: () => ipcRenderer.send('minimize-window'),
  maximizeWindow: () => ipcRenderer.send('maximize-window'),
  getUserInfo: () => ipcRenderer.invoke('get-user-info'),
  updateUserInfo: (newUserInfo) => ipcRenderer.send('update-user-info', newUserInfo),
  onUserInfoUpdated: (callback) => ipcRenderer.on('user-info-updated', (event, data) => callback(data)),
  writeData: (data) => ipcRenderer.invoke('write-data', data),
  openExternal: (url) => ipcRenderer.send("open-external", url),
  saveFile: () => ipcRenderer.invoke('save-file')
});
